﻿using System;
using System.IO;
using System.Text;
using log4net;
using System.Web;
using GMS.Core.Config;
using System.Web.Script.Serialization;

namespace GMS.Core.Log
{


    public class Log4NetHelper
    {
        public enum LogType
        { 
            Default,
            Timed,
            Delayed
        }

        static Log4NetHelper()
        {
            var dir = AppDomain.CurrentDomain.BaseDirectory;

            string path = Path.Combine(dir, "Config/log4net.config");
            
            FileInfo file = new FileInfo(path);
            SetConfig(file);
        }

        private static void SetConfig(FileInfo configFile)
        {   
            log4net.Config.XmlConfigurator.Configure(configFile);
        }

        private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        private static readonly log4net.ILog loginfo_time = log4net.LogManager.GetLogger("loginfo_time");
        private static readonly log4net.ILog loginfo_delay = log4net.LogManager.GetLogger("loginfo_delay");


        private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
        private static readonly log4net.ILog logerror_time = log4net.LogManager.GetLogger("logerror_time");
        private static readonly log4net.ILog logerror_delay = log4net.LogManager.GetLogger("logerror_delay");


        public static void Info(string info)
        {
            Info(LogType.Default, info);
        }

        public static void Info(LogType logType,string info)
        {
            lock (typeof(Log4NetHelper))
            {

                if(logType == LogType.Default)
                {
                    if (loginfo.IsInfoEnabled)
                    {
                        loginfo.Info(info);
                    }
                }
                else if (logType == LogType.Timed)
                {
                    if (loginfo_time.IsInfoEnabled)
                    {
                        loginfo_time.Info(info);
                    }
                }
                else if (logType == LogType.Delayed)
                {
                    if (loginfo_delay.IsInfoEnabled)
                    {
                        loginfo_delay.Info(info);
                    }
                }
            }
        }

        public static void Error(LogType logType, string info, Exception se)
        {
            lock (typeof(Log4NetHelper))
            {
                if (logType == LogType.Default)
                {
                    if (logerror.IsErrorEnabled)
                    {
                        logerror.Error(info, se);
                    }
                }
                else if (logType == LogType.Timed)
                {
                    if (logerror_time.IsErrorEnabled)
                    {
                        logerror_time.Error(info, se);
                    }
                }
                else if (logType == LogType.Delayed)
                {
                    if (logerror_delay.IsErrorEnabled)
                    {
                        logerror_delay.Error(info, se);
                    }
                }
            }
        }

        public static void Error(string info, Exception se)
        {
            Error(LogType.Default, info, se);
        }


        /// <summary>
        /// 记录接口的输入信息
        /// </summary>
        /// <param name="description">描述信息</param>
        /// <param name="args">参数</param>
        public static void LogInput(LogType logType, string description, params object[] args)
        {
            lock (typeof(Log4NetHelper))
            {
                string IsLogInput = "true";

                JavaScriptSerializer serializer = new JavaScriptSerializer();

                if (IsLogInput.ToLower() == "true")
                {
                    StringBuilder sbText = new StringBuilder();

                    sbText.AppendLine("描述信息:" + description);

                    string className = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name;
                    sbText.AppendLine("调用方法:" + className);

                    int index = 1;
                    foreach (object arg in args)
                    {
                        if (arg == null)
                            sbText.AppendLine("参数" + index + ": null ");
                        else
                            sbText.AppendLine("参数" + index + ":" + serializer.Serialize(arg));
                        index += 1;
                    }

                    Info(logType,sbText.ToString());

                }
            }
        }

        /// <summary>
        /// 记录接口的输入信息
        /// </summary>
        /// <param name="description">描述信息</param>
        /// <param name="args">参数</param>
        public static void LogInput(string description, params object[] args)
        {
            LogInput(LogType.Default, description, args);
        }


        /// <summary>
        /// 记录接口的输入信息
        /// </summary>
        /// <param name="description">描述信息</param>
        /// <param name="args">参数</param>
        public static void LogErrorInput(LogType logType, Exception ex, string description, params object[] args)
        {   
            lock (typeof(Log4NetHelper))
            {

                string IsLogInput = "true";

                JavaScriptSerializer serializer = new JavaScriptSerializer();

                if (IsLogInput.ToLower() == "true")
                {
                    StringBuilder sbText = new StringBuilder();

                    sbText.AppendLine("描述信息:" + description);

                    string className = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name;
                    sbText.AppendLine("调用方法:" + className);

                    int index = 1;
                    foreach (object arg in args)
                    {
                        if (arg == null)
                            sbText.AppendLine("参数" + index + ": null ");
                        else
                            sbText.AppendLine("参数" + index + ":" + serializer.Serialize(arg));
                        index += 1;
                    }

                    Error(logType,sbText.ToString(), ex);
                }
            }
        }

        
        /// <summary>
        /// 记录接口的输入信息
        /// </summary>
        /// <param name="description">描述信息</param>
        /// <param name="args">参数</param>
        public static void LogErrorInput(Exception ex, string description, params object[] args)
        {   
            LogErrorInput(LogType.Default, ex, description, args);
        }


    }
}
